import pygame
import sys
import random

# 初始化Pygame
pygame.init()

# 设置窗口大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置标题
pygame.display.set_caption("飞行棋游戏")

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

# 游戏参数
num_players = 4  # 玩家数量
dice_max = 6  # 骰子最大点数

# 初始化棋子位置
player_positions = [0 for _ in range(num_players)]
player_colors = [red, green, blue, yellow]


# 绘制游戏界面
def draw_game_interface():
    screen.fill(white)
    # 绘制飞行棋路径
    for i in range(1, 57):  # 假设一圈56格
        if i % 2 == 0:
            color = black
        else:
            color = (128, 128, 128)  # 灰色
        pygame.draw.circle(screen, color,
                           (int(screen_width / 2 + (i % 8 - 4) * 20), int(screen_height / 2 + (i // 8 - 2) * 20)), 10)

    # 绘制棋子
    for index, position in enumerate(player_positions):
        if position > 0:
            pygame.draw.circle(screen, player_colors[index], (
            int(screen_width / 2 + (position % 8 - 4) * 20), int(screen_height / 2 + (position // 8 - 2) * 20)), 15)


# 主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:  # 空格键投掷骰子
                dice_roll = random.randint(1, dice_max)
                print(f"Dice roll: {dice_roll}")
                for i in range(num_players):
                    if player_positions[i] == 0 or dice_roll == 6:  # 只有当棋子在起点时或投掷到6才能移动
                        player_positions[i] += dice_roll
                        if player_positions[i] >= 56:  # 到达终点
                            player_positions[i] = 0  # 重新开始
                            print(f"Player {i + 1} wins!")
                        break

    draw_game_interface()
    pygame.display.flip()

# 清理并退出Pygame
pygame.quit()
sys.exit()